package main;

import script.Script;
import ui.Window;
import define.Common;
import define.Config;
import define.WarningDefine;
import excelBase.ExcelFile;

public class WarningHandler 
{
	private static boolean m_hasWarning = false;
	private WarningDefine m_warning;
	private String m_makeup;
	private int m_position;
	private Script m_warningScript;
	private ExcelFile m_warningExcel;
	
	public WarningHandler(WarningDefine warning)
	{
		m_hasWarning = true;
		m_warning = warning;
		m_makeup = null;
		m_position = Config.NO_POSITION;
		m_warningScript = ErrorHandler.m_errorScript;
		m_warningExcel = ErrorHandler.m_errorExcel;
	}
	
	public WarningHandler(WarningDefine warning, int position)
	{
		m_hasWarning = true;
		m_warning = warning;
		m_makeup = null;
		m_position = position;
		m_warningScript = ErrorHandler.m_errorScript;
		m_warningExcel = ErrorHandler.m_errorExcel;
	}
	
	public WarningHandler(WarningDefine warning, String makeup)
	{
		m_hasWarning = true;
		m_warning = warning;
		m_makeup = makeup;
		m_position = Config.NO_POSITION;
		m_warningScript = ErrorHandler.m_errorScript;
		m_warningExcel = ErrorHandler.m_errorExcel;
	}
	
	public WarningHandler(WarningDefine warning, String makeup, int position)
	{
		m_hasWarning = true;
		m_warning = warning;
		m_makeup = makeup;
		m_position = position;
		m_warningScript = ErrorHandler.m_errorScript;
		m_warningExcel = ErrorHandler.m_errorExcel;
	}
	
	private String makeWarningMessage()
	{
		String name = "";
		if(m_warningScript != null)
		{
			name = "excel: " + m_warningScript.getExcel().getName() + "   script: " + m_warningScript.getName() + "\n";
		}
		else if(m_warningExcel != null)
		{
			name = "excel: " + m_warningExcel.getName() + "\n";
		}
		name += "warning: " + m_warning.getMessage() + (m_makeup == null ? "" : ": " + m_makeup);
		String pos = "";
		if(m_position != Config.NO_POSITION)
		{
			try 
			{
				pos = getPositionString(m_position);
			} 
			catch(Exception e2)
			{
				pos = "     can't locate error position";
			}
		}
		return name + pos;
	}
	
	public void showWarning()
	{
		if(Common.m_hasUI)
		{
			Window.m_instance.addText(makeWarningMessage());
		}
		else
		{
			System.out.println(makeWarningMessage());
		}
	}
	
	public boolean hasWarning()
	{
		return m_hasWarning;
	}
	
	public int getPosition()
	{
		return m_position;
	}
	
	private String getPositionString(int position) throws Exception
	{
		int start = m_warningScript.getRealPosition(position);
		int end = position + Config.ERROR_STRING_LEN;
		int[] tempArray = m_warningScript.readFile();
		if(end >= tempArray.length)
		{
			end = tempArray.length - 1;
		}
		int line = 1;
		int lineEnd = -1;
		for(int i = 0; i < start; ++i)
		{
			if(tempArray[i] == '\r')
			{
				++line;
				if(i < tempArray.length - 1 && tempArray[i + 1] == '\n')
				{
					++i;
				}
				lineEnd = i;
			}
			else if(tempArray[i] == '\n')
			{
				++line;
				lineEnd = i;
			}
		}
		lineEnd = start - lineEnd;
		String error = "-- ";
		for(int i = start; i <= end; ++i)
		{
			error += (char)tempArray[i];
		}
		error += " --";
		return "   line:" + line + ", " + lineEnd + ": " + error;
	}
}
